import os, random

example = 1

setGlobalDuration(-1)

tab = genDataTable([0]*513)

if '/Ounk.app' not in os.getcwd():
    myfile = os.path.join(os.getcwd(), 'doc', 'examples', 'FFTfilter.txt')
else:
    myfile = os.path.join(os.getcwd(), '..', '..', '..', 'doc', 'examples', 'FFTfilter.txt')

myInstrument(myfile, argslist=[tab], out=1)

reverb(input=1)

proc = startCsound()

def filt(list):
    list = list + [0] * (513-len(list))
    reGenDataTable(tab, list, proc)

# Control the filter with a multiSlider
if example == 1:
    frame = beginGUI('FFT Filter', (820,250))
    makeMultiSlider(frame, label='FFT Filter', init=[0]*100, sliders=100, 
                    pos=(10,10), size=(800,180), function=filt)
    endGUI(frame)

# Little algorithm to randomly generate sequential filters
if example == 2:
    choice = range(100)
    n1 = n2 = n3 = n4 = 0
    t1 = t2 = t3 = t4 = 0
    def makeList():
        global n1, n2, n3, n4, t1, t2, t3, t4
        beat = pat.getBeat()
        if beat == 1:
            nums = random.sample(choice, 4)
            n1, n2, n3, n4 = nums[0], nums[1], nums[2], nums[3]
            t1 = random.uniform(.5,1) * mapper(n1,0,100,2,.0001)
            t2 = random.uniform(.5,1) * mapper(n2,0,100,2,.0001)
            t3 = random.uniform(.5,1) * mapper(n3,0,100,2,.0001)
            t4 = random.uniform(.5,1) * mapper(n4,0,100,2,.0001)
        l1 = mapper(beat, 1, 100, 0, t1)
        l2 = mapper(beat, 1, 100, 0, t2)
        l3 = mapper(beat, 1, 100, 0, t3)
        l4 = mapper(beat, 1, 100, 0, t4)    
        list = [0]*513
        for i in ((n1,l1),(n2,l2),(n3,l3),(n4,l4)):
            list[i[0]] = i[1]
        reGenDataTable(tab, list, proc) 

    pat = pattern(.02, makeList, [1]*100)
    pat.start()
    pat.play()
